<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0px;
        padding: 0px;
        text-decoration: none;
    }

    .bg-gradient {
        position: fixed;
        height: 100%;
        width: 50%;
        opacity: 30%;
        z-index: -999;
    }

    .container {
        position: absolute;
        height: 300px;
        z-index: 999;
        background: rgb(70, 90, 240,10%);
    }

    #barChart {
        /* width: 1000px; */
    }

    .left_line {
        z-index: 999;
        background: skyblue;
        height: 120%;
        width: 10px;
        position: absolute;
        top: 30px;
    }
</style>


<style>
    /* 头部tab */
    .navbar {
        margin: 10px;
        /* box-shadow:0px 5px 20px rgb(58, 47, 47); */
        width: 25%;
        height: 50px;
        border-radius: 10px;
        line-height: 100px;
        position: fixed;
        left: 35%;
        top: -30px;
    }

    .row {
        display: flex;
        text-align: center;
        justify-content: center;
        position: relative;
        width: 100%;
    }

    .row-item {
        font-size: 17px;
        /* margin: 5px; */
        width: 33%;
        color: black;
    }

    .active {
        width: 33%;
        background-color: rgb(56, 134, 197);
        border-radius: 10px;
        transition: .2s;
    }

    .underline {

        bottom: 10px;
        /* right: 1%; */
        transition: .5s;
        position: absolute;
        height: 5px;
        width: 33%;
        background-color: rgb(56, 134, 197);

    }

    /* nth是匹配父元素选择器的 */
    .row-item:nth-child(1):hover~.underline {
        left: calc(100% / 3 *0);
        /* background: red; */
    }

    .row-item:nth-child(2):hover~.underline {
        left: calc(100% / 3 *1);

    }

    .row-item:nth-child(3):hover~.underline {
        left: calc(100% / 3 *2);

    }
</style>

<body>

    <div class="navbar">
        <div class="row">


            <a href="#" class="row-item">数据总览</a>
            <a href="#" class="row-item">规划浏览</a>
            <a href="#" class="row-item">企业信息</a>
            <span class="underline"></span>
        </div>
    </div>
    <!-- <img class="bg-gradient" src="background.png">

    </img> -->
    <div height="310" width="400" style="margin:50px" class="container">
 

        </span>
        <canvas id="barChart"> </canvas>
    </div>


</body>
<script>

    function goBarChart(dataArr) {
        // 声明所需变量
        var canvas, ctx;
        // 图表属性
        var cWidth, cHeight, cMargin, cSpace;
        var originX, originY;
        // 柱状图属性
        var bMargin, tobalBars, bWidth, maxValue;
        var totalYNomber;
        var gradient;

        // 运动相关变量
        var ctr, numctr, speed;
        //鼠标移动
        var mousePosition = {};

        // 1.获得canvas上下文
        canvas = document.getElementById("barChart");
        if (canvas && canvas.getContext) {
            ctx = canvas.getContext("2d");
        }
        // canvas.style.cssText="background:linear-gradient(to right, rgb(60,50,50), transparent)"
        initChart(); // 图表初始化
        drawLineLabelMarkers(); // 绘制图表轴、标签和标记
        drawBarAnimate(); // 绘制柱状图的动画
        //检测鼠标移动
        var mouseTimer = null;
        canvas.addEventListener("mousemove", function (e) {
            e = e || window.event;
            if (e.offsetX || e.offsetX == 0) {
                mousePosition.x = e.offsetX;
                mousePosition.y = e.offsetY;
            } else if (e.layerX || e.layerX == 0) {
                mousePosition.x = e.layerX;
                mousePosition.y = e.layerY;
            }

            clearTimeout(mouseTimer);
            mouseTimer = setTimeout(function () {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                drawLineLabelMarkers();
                drawBarAnimate(true);
            }, 10);
        });

        //点击刷新图表
        canvas.onclick = function () {
            initChart(); // 图表初始化
            drawLineLabelMarkers(); // 绘制图表轴、标签和标记
            drawBarAnimate(); // 绘制折线图的动画
        };


        // 图表初始化
        function initChart() {
            // 图表信息
            cMargin = 60;
            cSpace = 100;
            /*
                这里是对高清屏幕的处理，
                  方法：先将canvas的width 和height设置成本来的两倍
                  然后将style.height 和 style.width设置成本来的宽高
                  这样相当于把两倍的东西缩放到原来的 1/2，这样在高清屏幕上 一个像素的位置就可以有两个像素的值
                  这样需要注意的是所有的宽高间距，文字大小等都得设置成原来的两倍才可以。
            */
            canvas.width = canvas.parentNode.getAttribute("width") * 2;
            canvas.height = canvas.parentNode.getAttribute("height") * 2;
            canvas.style.height = canvas.height / 2 + "px";
            canvas.style.width = canvas.width / 2 + "px";
            cHeight = canvas.height - cMargin - cSpace;
            cWidth = canvas.width - cMargin - cSpace;
            originX = cMargin + cSpace;
            originY = cMargin + cHeight;

            // 柱状图信息
            bMargin = canvas.width / 40;
            tobalBars = dataArr.length;
            bWidth = parseInt(cWidth / tobalBars - bMargin);
            maxValue = 0;
            for (var i = 0; i < dataArr.length; i++) {
                var barVal = parseInt(dataArr[i][1]);
                if (barVal > maxValue) {
                    maxValue = barVal;
                }
            }
            maxValue += 50;
            totalYNomber = 10;
            // 运动相关
            ctr = 1;
            numctr = 100;
            speed = 10;

            //柱状图渐变色 createLinearGradient(渐变开始点的 x，y 坐标，渐变结束点的 x，y 坐标  )
            // 这里我们要倒着看
            gradient = ctx.createLinearGradient(0, 300, 0, 600);
            gradient.addColorStop(0, 'skyblue');
            gradient.addColorStop(1, 'transparent');

        }

        // 绘制图表轴、标签和标记
        function drawLineLabelMarkers() {
            //ctx.translate(0.5,0.5);  // 当只绘制1像素的线的时候，坐标点需要偏移，这样才能画出1像素实线
            ctx.font = "24px 微软雅黑";
            // 内置线的宽度
            ctx.lineWidth = 1;
            ctx.fillStyle = "#000";
            ctx.strokeStyle = "#000";
            // y轴
            drawLine(originX, originY, originX, cMargin);
            // x轴
            drawLine(originX, originY, originX + cWidth, originY);

            // 绘制标记
            drawMarkers();
            //ctx.translate(-0.5,-0.5);  // 还原位置
        }

        // 画线的方法
        function drawLine(x, y, X, Y) {
            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(X, Y);
            ctx.stroke();
            ctx.closePath();
        }

        // 绘制标记
        function drawMarkers() {
            ctx.strokeStyle = "#E0E0E0";
            // 绘制 y
            var oneVal = parseInt(maxValue / totalYNomber);
            ctx.textAlign = "right";
            for (var i = 0; i <= totalYNomber; i++) {
                var markerVal = i * oneVal;
                var xMarker = originX - 10;
                var yMarker = parseInt(cHeight * (1 - markerVal / maxValue)) + cMargin;
                if (i % 2 == 0) {
                    // y轴 样式减少（统一修改）
                    ctx.fillText(markerVal * 1.5, xMarker, yMarker + 3, cSpace); // 文字
                }

                if (i > 0) {
                    //绘制里面的线
                    drawLine(originX + 2, yMarker, originX + cWidth, yMarker);
                }
            }
            // 绘制 x
            ctx.textAlign = "center";
            for (var i = 0; i < tobalBars; i++) {
                var markerVal = dataArr[i][0];
                var xMarker = parseInt(originX + cWidth * (i / tobalBars) + bMargin + bWidth / 2);
                var yMarker = originY + 30;
                ctx.fillText(markerVal, xMarker, yMarker, cSpace); // 文字
            }
            // 绘制标题 y
            ctx.save();
            ctx.rotate(-Math.PI / 2);
            // ctx.fillText("产 量", -canvas.height / 2, cSpace - 10);
            ctx.restore();
            // 绘制标题 x
            // ctx.fillText("年份", originX + cWidth / 2, originY + cSpace / 2 + 30);
        };

        //绘制柱形图
        function drawBarAnimate(mouseMove) {
            for (var i = 0; i < tobalBars; i++) {
                var oneVal = parseInt(maxValue / totalYNomber);
                // y轴 样式减少（统一修改）
                var barVal = dataArr[i][1] / 1.5;
                var barH = parseInt(cHeight * barVal / maxValue * ctr / numctr);
                var y = (originY - barH);
                var x = originX + (bWidth + bMargin) * i + bMargin;
                drawRect(x, y, bWidth, (barH - 1), mouseMove);  //高度减一避免盖住x轴
                // y轴 样式减少（统一修改）
                ctx.fillText(parseInt(barVal * 1.5 * ctr / numctr), x + 30, y - 8); // 文字
            }
            if (ctr < numctr) {
                ctr++;
                setTimeout(function () {
                    ctx.clearRect(0, 0, canvas.width, canvas.height);
                    drawLineLabelMarkers();
                    drawBarAnimate();
                }, speed);
            }
        }

        //绘制方块
        function drawRect(x, y, X, Y, mouseMove) {

            ctx.beginPath();
            //核心方法

            ctx.rect(x, y, X, Y);

            if (mouseMove && ctx.isPointInPath(mousePosition.x * 2, mousePosition.y * 2)) { //如果是鼠标移动的到柱状图上，重新绘制图表
                ctx.fillStyle = "skyblue";

            } else {
                ctx.fillStyle = gradient;
                ctx.strokeStyle = gradient;
            }
            ctx.fill();
            ctx.closePath();

        }


    }


    goBarChart(
        [[1, 750], [2, 425], [3, 960], [4, 700], [5, 800], [6, 975], [7, 375], [8, 775]]
    )


</script>

</html>